package com.rybbaby.tss.front.tssMemberPackage.service.imp;

import com.rybbaby.tss.core.utils.DateUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import com.rybbaby.tss.front.tssMemberPackageHistory.entity.TssMemberPackageHistory;
import com.rybbaby.tss.front.tssPackage.dao.ITssPackageDao;
import com.rybbaby.tss.front.tssPackage.entity.TssPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author 雷春琼
 * @description 会员课时包数据库操作服务实现
 * @createDate 2017-08-10 上午12:04:35
 */
@Service
@Transactional
public class TssMemberPackageServiceImp extends BaseServiceImp<TssMemberPackage> implements ITssMemberPackageService {

    @Autowired
    private ITssMemberPackageDao dao;
    @Autowired
    private ITssPackageDao packageDao;

    /**
     * 将会员课时包计数字段归0
     * @param memberPackage
     */
    public void clearMemberPackagerCountCorrelation(TssMemberPackage memberPackage){
        memberPackage.setGiftAttendanceDay(0);
        memberPackage.setAttendanceDay(0);
        memberPackage.setAttendedDay(0);
        memberPackage.setAbsentDay(0);
        memberPackage.setSurplusAttendanceDay(0);
        memberPackage.setSurplusFormalAttendanceDay(0);
        memberPackage.setSurplusPresentAttendanceDay(0);
        memberPackage.setSelectedAttendanceDay(0);
        memberPackage.setConsumptionAttendanceDay(0);
        memberPackage.setPositiveConsumptionAttendanceDay(0);
        memberPackage.setGiftConsumptionAttendanceDay(0);
        memberPackage.setUnusedAttendanceDay(0);
        memberPackage.setPositiveUnusedAttendanceDay(0);
        memberPackage.setGiftUnusedAttendanceDay(0);
        memberPackage.setPresentClassHour(0);
        memberPackage.setUsableTotalClassHour(0);
        memberPackage.setSurplusClassHour(0);
        memberPackage.setSurplusFormalClassHour(0);
        memberPackage.setSurplusPresentClassHour(0);
        memberPackage.setSelectedClassHour(0);
        memberPackage.setConsumptionClassHour(0);
        memberPackage.setPositiveConsumptionClassHour(0);
        memberPackage.setGiftConsumptionClassHour(0);
        memberPackage.setUnusedClassHour(0);
        memberPackage.setPositiveUnusedClassHour(0);
        memberPackage.setGiftUnusedClassHour(0);
        memberPackage.setRemediationClassHour(0);
        memberPackage.setUsableTotal(0);
        memberPackage.setSurplusValidDate(0);
        memberPackage.setPackageStatus(3);
        memberPackage.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        this.updateEntitiy(memberPackage);

    }
    /**
     * 根据会员id查询购买课时包信息
     *
     * @param memberId
     * @return
     */
    @Override
    public List<Map<String, Object>> getListByMemberId(String memberId, Integer type) throws Exception {
        return dao.getListByMemberId(memberId, type);
    }

    /**
     * 修改会员课时包状态
     *
     * @param packageStatus
     * @return
     */
    @Override
    public Integer updatePackageStatus(String targetDateId, Integer packageStatus) {
        return dao.updatePackageStatus(targetDateId, packageStatus);
    }

    @Override
    public Integer updateParameterById(Map<String, Object> map, String id) throws Exception {
        if (map.get("packageStatus") != null && StringUtils.isNotBlank(map.get("packageStatus").toString())) {
            Integer status = Integer.valueOf(map.get("packageStatus").toString());
            if (status == 1) {
                //查询课时包有效期
                TssMemberPackage memberPackage = this.get(id);
                TssPackage tssPackage = packageDao.get(TssPackage.class, memberPackage.getPackageId());
                Integer validDate = tssPackage.getValidDate();
                //计算距离到期时间天数
                Date date = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.MONTH, validDate);
                date = cal.getTime();
                //date.setMonth(date.getMonth()+validDate);
                String endTime = DateUtil.format(date, "yyyy-MM-dd");
                String startTime = DateUtil.format(new Date(), "yyyy-MM-dd");
                Integer surplusValidDate = DateUtil.getAllDay(startTime, endTime, "yyyy-MM-dd").size();
                //添加有效期
                map.put("dueTime", endTime);
                map.put("surplusValidDate", surplusValidDate);
            }
        }

        return dao.updateParameterById(map, id);
    }


    @Override
    public boolean deductiveHours(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.deductiveHours(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean deductiveAttendanceDay(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.deductiveAttendanceDay(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean absenteeismDeductiveHours(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.absenteeismDeductiveHours(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean absenteeismDeductiveAttendanceDay(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.absenteeismDeductiveAttendanceDay(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean makeUpClassDeductiveHours(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.makeUpClassDeductiveHours(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean batchDeductiveHours(List<Map<String, Object>> packageInformation) {
        ArrayList<Object[]> params = new ArrayList<>();
        for (Map<String, Object> map : packageInformation) {
            Object formalClass = map.get("formalClass");
            Object giftClass = map.get("giftClass");
            Object memberPackageId = map.get("memberPackageId");
            params.add(new Object[]{formalClass, giftClass, formalClass, giftClass, formalClass, giftClass, formalClass, giftClass, memberPackageId});
        }
        boolean i = dao.batchDeductiveHours(params);
        return i;
    }

    @Override
    public boolean batcheductiveAttendanceDay(List<Map<String, Object>> packageInformation) {
        ArrayList<Object[]> params = new ArrayList<>();
        for (Map<String, Object> map : packageInformation) {
            Object formalClass = map.get("formalClass");
            Object giftClass = map.get("giftClass");
            Object memberPackageId = map.get("memberPackageId");
            params.add(new Object[]{formalClass, giftClass, formalClass, giftClass, formalClass, giftClass, formalClass, giftClass, memberPackageId});
        }
        boolean i = dao.batcheductiveAttendanceDay(params);
        return i;
    }

    @Override
    public boolean reductionMakeUpClassDeductiveHours(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.reductionMakeUpClassDeductiveHours(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean reductionAbsenteeismDeductiveHours(Map<String, Object> packageInformation) {
        boolean j = false;

        int i = dao.reductionAbsenteeismDeductiveHours(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean addMakeUpClassDeductiveHours(Map<String, Object> packageInformation) {
        boolean j = false;
        int i = dao.addMakeUpClassDeductiveHours(packageInformation);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public Serializable saveMemberPackage(TssMemberPackage saveMemberPackage) {
        return dao.save(saveMemberPackage);
    }

    @Override
    public Map<String, Object> findApplyStatus(Integer type, String memberId) {
        return dao.findApplyStatus(type, memberId);
    }

    @Override
    public List<TssMemberPackage> getAllList() {
        return (List<TssMemberPackage>) this.dao.loadAll(TssMemberPackage.class);
    }

    @Override
    public List<TssMemberPackage> findByMemberId(String targetDataId) throws Exception {
        return this.dao.findByMemberId(targetDataId);
    }

    @Override
    public boolean attendanceMaintenanceForParentingPackage(Map<String, Object> attendanceMaintenancePackageMap) {
        boolean j = false;
        int i = dao.attendanceMaintenanceForParentingPackage(attendanceMaintenancePackageMap);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public boolean attendanceMaintenanceForTransitionPackage(Map<String, Object> attendanceMaintenancePackageMap) {
        boolean j = false;
        int i = dao.attendanceMaintenanceForTransitionPackage(attendanceMaintenancePackageMap);
        if (i != 0) {
            j = true;
        }
        return j;
    }

    @Override
    public void updateMemberPackageEntitiy(TssMemberPackage memberPackage) {
        dao.updateEntitiy(memberPackage);
    }

    @Override
    public List<TssMemberPackage> findHistory(List<String> ids) throws Exception {
        String paramId = "'";
        for (int j = 0; j < ids.size(); j++) {
            if (j == (ids.size() - 1)) {
                paramId = paramId + ids.get(j) + "'";
            } else {
                paramId = paramId + ids.get(j) + "','";
            }
        }
        return dao.findHistory(paramId);
    }

    @Override
    public List<Map<String, Object>> finByHistory(String startTime_begin, String Type) {
        return dao.finByHistory(startTime_begin, Type);
    }

    @Override
    public void absenceRecord(Map<String, Object> packageInformation) {
        dao.absenceRecord(packageInformation);
    }


}
